package com.gsit.ocraspect.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.HttpSessionRequiredException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.gsit.ocraspect.services.BatchService;
import com.gsit.ocrbatch.domain.Record;
import com.gsit.ocrbatch.domain.RecordReject;
import com.gsit.ocrbatch.domain.RecordState;
import com.gsit.ocrbatch.domain.RejectState;
import com.gsit.ocrbatch.domain.WebConfig;
import com.gsit.ocrbatch.services.EmailService;
import com.gsit.ocrbatch.services.MessageConfigService;
import com.gsit.ocrbatch.services.RecordService;
import com.gsit.ocrbatch.utils.Constant;
import com.gsit.ocrbatch.utils.FileConstant.FileFlow;
import com.gsit.ocrbatch.utils.MessageUtil;



/**
 * Handles requests for the application home page.
 */
@Controller
@SessionAttributes({Constant.LOGIN,Constant.WEBCONFIG})
public class HomeController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@ModelAttribute("records")
	public List<Record> getAllAddresses() {
	  return recordService.findByAllWaitingRecords();
	}
	
	@ModelAttribute("allRecordRejects")
	public List<RecordReject> getAllRecordRejects() {
		return MessageUtil.getAllRecordRejects();
	}
	
	
	@Autowired
	private RecordService recordService; 
	
	@Autowired
	private MessageConfigService messageConfigService; 
	
	@Autowired
	private EmailService emailService;
	
	@Autowired
	private BatchService batchService;
	
	
	@Autowired
	private WebConfig webConfig;

	
	
	/**
	 * Simply selects the home view to render by returning its name.
	 */
	@RequestMapping(value = "/home", method = RequestMethod.GET)
	public String home(Locale locale, Model model) {
		logger.info("Welcome home! the client locale is "+ locale.toString());
		return "home";
	}
	
	
	
	@RequestMapping(value = "/display/{id}")
	public String display(@PathVariable Long id,Model model) {
		logger.info("dispaly call ");
		
		Record record = recordService.findById(id);
		
		
		//record.setRecordRejects(recordRejects);
		
		System.out.println("The new is "+record.getName());
		//model.addAttribute("allRecordRejects",  recordRejects);
		model.addAttribute("record",  record);
		model.addAttribute("name", record.getName() );
		model.addAttribute("webConfig", webConfig);
		
		
		return "display";
	}
	
	@RequestMapping(value = "/changeStatus/", method = RequestMethod.POST)
	public String displaySubmit(Record record,Locale locale, Model model) {
		
		
		
		if(RecordState.REJECT==record.getRecordState()){
			
			for (RecordReject recordReject : record.getRecordRejects()) {
				System.out.println(">>>>>>>>>>>>"+recordReject.getRejectState());
			}
			batchService.process(FileFlow.ERROR, record);
		}else{
			record.setRecordRejects(new ArrayList<RecordReject>());
			batchService.process(FileFlow.OK, record);
		}
		
		
		//recordService.updateRecordState(record);
		
		if(RecordState.REJECT==record.getRecordState()){
			messageConfigService.send(record);
			emailService.sendMailToAll(record);
		}
		
		getAllAddresses();
		return "redirect:/home";
	}
	
	@RequestMapping(value = "/contact")
	public String contact(Locale locale, Model model) {
		return "contact";
	}
	
	@RequestMapping(value = "/issues", method = RequestMethod.GET)
	public String issues(Locale locale, Model model) {
		return "issues";
	}
	
	
	@RequestMapping(value="/getAllRecords", method = RequestMethod.GET)	
	public @ResponseBody List<Record> getAllRecords(){
		return recordService.findByAllWaitingRecords();
	}
	
	
	@ExceptionHandler(HttpSessionRequiredException.class)
	public String reset() {
		return "redirect:/index";
	}
}
